/**
 * 
 */
package net.sahv.bdyz.controller.admin;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import net.sahv.bdyz.model.Admin;
import net.sahv.bdyz.model.AdminRole;
import net.sahv.bdyz.service.AdminRoleService;
import net.sahv.bdyz.service.AdminService;
import net.sahv.bdyz.service.MemberAttentionService;
import net.sahv.bdyz.service.MemberLoginInfoService;
import net.sahv.bdyz.util.PagingUtil;

/**
 * @author 张天敏
 * @date 日期：2016年9月28日 时间：上午9:04:05
 */
@Controller("memberAttentionController")
@RequestMapping("admin/memberAttention")
public class MemberAttentionController {
	
	@Autowired
	private MemberAttentionService memberAttentionService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private AdminRoleService adminRoleService;// 角色管理员关系
	@Autowired
	private MemberLoginInfoService memberLoginInfoService;
	
	@RequestMapping(value="/memberAttentionDayList",method=RequestMethod.GET)
	public String memberAttentionDayList(Model model,String attType,String attentionDate,String flag){
		model.addAttribute("attType", attType);
		model.addAttribute("attentionDate", attentionDate);
		model.addAttribute("flag", flag);
		return "admin/memberAttention/memberAttentionDayList";
	}
	
	/**
	 * 会员访问排名列表（日）
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：上午9:42:46
	 * @param request
	 * @param pagingUtil
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value="/memberAttentionDayList",method=RequestMethod.POST)
	public JSON memberAttentionDayList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			//用户名
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			//手机号
			String M_PHONE=request.getParameter("M_PHONE");
			//编号
			String NO=request.getParameter("NO");
			//关注日期
			String attentionDate=request.getParameter("attentionDate");
			//关注类型 1面料 2花型
			String attType=request.getParameter("attType");
			//flag：1、日；2、周；3、月
			String flag=request.getParameter("flag");
			
			String dateSql="";
			String showColumns="";
			String leftTable="";
			String groupByParam="";
			
			if(flag==null||flag.trim().equals("")){
				flag="1";
			}
			
			if(attType!=null&&!attType.trim().equals("")&&attType.trim().equals("1")||attType.trim().equals("3")){
				showColumns="  ,f.`F_NAME`,f.`F_NO` as NO ";
				leftTable=" LEFT JOIN `fabric` f ON ma.`attId`=f.`F_ID` ";
				groupByParam=" ,f.`F_NO` ";
			}else if(attType!=null&&!attType.trim().equals("")&&attType.trim().equals("2")||attType.trim().equals("4")){
				showColumns=" ,p.`P_NO` as NO ";
				leftTable=" LEFT JOIN `pattern` p ON ma.`attId`=p.`P_ID` ";
				groupByParam=" ,p.`P_NO` ";
			}
			
			if(flag.equals("1")){
				dateSql=" DATE_FORMAT(ma.`createTime`,'%Y-%m-%d') ";
			}else if(flag.equals("2")){
				dateSql=" YEARWEEK(ma.`createTime`) ";
			}else if(flag.equals("3")){
				dateSql=" DATE_FORMAT(ma.`createTime`,'%Y-%m') ";
			}
			
			String tableSql=" SELECT ma.`mId`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberAttention,"+dateSql+" AS attentionDate "+showColumns+" FROM `member_attention` ma LEFT JOIN `member` m ON ma.`mId`=m.`M_ID`  "+leftTable;

			String sortField = " GROUP BY "+dateSql+groupByParam+" ORDER BY COUNT(m.`M_ID`) DESC,"+dateSql+" DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(NO!=null&&!NO.trim().equals("")){
				if(attType.equals("1")||attType.equals("3")){
					where+=" AND f.`F_NO` LIKE '%"+NO+"%' ";
				}else if(attType.equals("2")||attType.equals("4")){
					where+=" AND p.`P_NO` LIKE '%"+NO+"%' ";
				}
			}
			
			if(attentionDate!=null&&!attentionDate.trim().equals("")){
				if(flag.equals("2")){
					attentionDate=MemberSortController.parseDateToWeek(attentionDate);
				}
				where+=" AND "+dateSql+"='"+attentionDate+"' ";
			}
			
			if(attType!=null&&!attType.trim().equals("")){
				where+=" and ma.`attType`="+attType;
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}

}
